{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Composite Models\n", "\n", "Models can be created by using functors like `SumPDF`, `ProdPDF`.\n", "\n", "There are two ways to create such models, either with the class API or with simple Python syntax.\n", "\n", "## Sum PDF\n", "\n", "Lets compose a sum of two gaussians by first creating each gaussian and a fraction parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import zfit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "frac = zfit.Parameter(\"frac_gauss\", 0.5, 0, 1)\n", "\n", "obs1 = zfit.Space('obs1',-5, 5)\n", "\n", "mu1 = zfit.Parameter(\"mu1\", 1.)\n", "sigma1 = zfit.Parameter(\"sigma1\", 1.)\n", "gauss1 = zfit.pdf.Gauss(obs=obs1, mu=mu1, sigma=sigma1)\n", "\n", "mu2 = zfit.Parameter(\"mu2\", 1.)\n", "sigma2 = zfit.Parameter(\"sigma2\", 1.)\n", "gauss2 = zfit.pdf.Gauss(obs=obs1, mu=mu2, sigma=sigma2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The sum can be created like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum_gauss = zfit.pdf.SumPDF(pdfs=[gauss1, gauss2], fracs=frac)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(sum_gauss.obs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hereby, the new pdf has the same observables as the daughter pdfs, as they coincide. If they do not, then they are combined (if there is no conflict with the limits). This can be useful to create higher dimensional pdfs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Product PDF\n", "\n", "Let's now create a 2D product of two gaussians. Again, we can choose between the Python syntax and the class API." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "obs2 = zfit.Space('obs2', -3, 7)\n", "mu3 = zfit.Parameter(\"mu3\", 1.)\n", "sigma3 = zfit.Parameter(\"sigma3\", 1.)\n", "gauss3 = zfit.pdf.Gauss(obs=obs2, mu=mu3, sigma=sigma3) # different obs than above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prod_gauss = zfit.pdf.ProductPDF(pdfs=[gauss1, gauss3])\n", "prod_gauss_inverted_order = zfit.pdf.ProductPDF(pdfs=[gauss3, gauss1]) # notice the different order of the pdfS!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_it is also possible to use the following code, but it should only be used for simple cases and is not recommended for more then two pdfs, since this leads to inefficinet, tree like product structures_:\n", "`prod_gauss = gauss1 * gauss3` # NOT RECOMMENDED FOR MORE THAN 2 PDFs!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The new pdf is now in two dimensions. The order of the observables follows the order of the pdfs given." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"python syntax product obs\", prod_gauss.obs)\n", "print(\"class API product obs\", prod_gauss_inverted_order.obs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## creating an extended PDF\n", "\n", "An extended PDF can be created using the `extended` argument in the initialization.\n", " \n", "Alternatively, an extended PDF from a non-extended PDF can be created with the `create_extended(yield_param)` method." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "yield1 = zfit.Parameter(\"yield_gauss1\", 100, 0, 1000)\n", "gauss3_ext = zfit.pdf.Gauss(obs=obs2, mu=mu3, sigma=sigma3, extended=yield1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "extended_gauss_method = gauss3.create_extended(yield1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 4 }